<template>
  <div class="container" :class="{ blue: !isVoting }">
    <!-- <div class="nav-title">投票</div> -->
    <el-form
      ref="form"
      class="form"
      label-position="top"
      :model="form"
      :rules="rules"
      size="medium"
      v-if="!isVoting"
    >
      <el-form-item label="投票节目" prop="programInfoId">
        <el-radio-group v-model="form.programInfoId">
          <div
            :class="{ 'mtb-20': index < programList.length - 1 }"
            v-for="(item, index) in programList"
            :key="item.id"
          >
            <el-radio :label="item.id">{{ item.programName }}</el-radio>
          </div>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="入场号码（手中号码球数字）" prop="votingNumber">
        <el-input
          class="voting-number-ipt"
          v-model.number="form.votingNumber"
          placeholder="请输入入场号码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <div style="padding-top: 15px">
          <el-button
            :disabled="loading"
            :loading="loading"
            style="width: 100%"
            type="danger"
            size="medium"
            round
            @click="onSubmit"
            >立即投票</el-button
          >
        </div>
      </el-form-item>
    </el-form>
    <div class="result-content" v-else>
      <i class="el-icon-check"></i>
      <p class="text">投票成功，感谢参与！</p>
    </div>
  </div>
</template>
<script>
import { getProgramVotingList, postProgramVoting } from "@/api/program-voting";
export default {
  name: "ProgramVoting",
  data() {
    return {
      form: {
        programInfoId: null,
        votingNumber: "",
      },
      rules: {
        programInfoId: [
          { required: true, message: "请选择投票节目", trigger: "change" },
        ],
        votingNumber: [
          { required: true, message: "请输入入场号码", trigger: "blur" },
          {
            pattern: /^([1-9]|[1-9]\d|1\d{2}|200)$/,
            message: "请输入正确的号码",
            trigger: "blur",
          },
        ],
      },
      sessionCode: "",
      programList: [],
      loading: false,
      isVoting: false,
    };
  },
  created() {
    const query = this.$route.query;
    if (query.code) {
      this.sessionCode = query.code;
      this.init();
      const code = localStorage.getItem("code");
      if (code && code === this.sessionCode) {
        this.isVoting = localStorage.getItem("isVoting") ? true : false;
      }
    }
  },
  mounted() {
    document.title = "节目投票";
  },
  methods: {
    init() {
      getProgramVotingList({
        code: this.sessionCode,
      }).then((res) => {
        if (res.code === 200) {
          this.programList = res.data;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          postProgramVoting({
            ...this.form,
            code: this.sessionCode,
          })
            .then((res) => {
              if (res.code === 200) {
                this.isVoting = true;
                localStorage.setItem("isVoting", 1);
                localStorage.setItem("code", this.sessionCode);
              } else {
                this.$message.error(res.msg);
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
  },
  watch: {},
};
</script>
<style lang="scss" scoped>
.container {
  min-height: 100vh;
  padding: 45px 15px 15px;
  width: 100%;
  overflow: hidden;
  background: url("../../assets/images/phone-bg.jpg") #ff3333 100% 100%
    no-repeat;
  // background-size: 100% 100%;
  // background-color: #ff3333;
  // &.blue {
  //   background: #ff3333;
  // }
}
.nav-title {
  line-height: 44px;
  text-align: center;
  font-size: 16px;
  background: #fff;
  border-bottom: 1px solid #ebeef5;
  // box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1);
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 999;
}
.form {
  background: #fff;
  margin-top: 15px;
  padding: 15px;
  border-radius: 5px;
  box-shadow: 1px 1px 20px rgba(0, 0, 0, 0.2);
}
.result-content {
  text-align: center;
  padding-top: 50px;
  i {
    font-size: 60px;
    color: #67c23a;
    font-weight: bold;
  }
  p {
    font-size: 22px;
    margin-top: 5px;
  }
}
.mtb-20 {
  margin-bottom: 20px;
}
.text {
  color: #fff;
}
::v-deep {
  .voting-number-ipt .el-input__inner {
    height: 70px;
    line-height: 70px;
    font-size: 30px;
    font-weight: bold;
    // width: 150px;
    &::placeholder {
      color: #999; /* 更改文本颜色 */
      font-size: 24px; /* 更改字体大小 */
      opacity: 0.5; /* 更改透明度 */
      font-weight: 400;
    }
  }
  .el-radio__label {
    font-size: 16px;
  }
  .el-button--medium.is-round {
    padding: 15px 20px;
  }
  .el-radio input[aria-hidden="true"] {
    display: none !important;
  }
  .el-radio:focus:not(.is-focus):not(:active):not(.is-disabled)
    .el-radio__inner {
    box-shadow: none !important;
  }
}
</style>
